using System.Linq;
using Microsoft.WindowsAzure.Diagnostics;
using Microsoft.WindowsAzure.ServiceRuntime;

namespace eDirectory.WcfService
{
    public class WebRole : RoleEntryPoint
    {
        public override bool OnStart()
        {
            // Get the factory configuration so that it can be edited 
            DiagnosticMonitorConfiguration config = DiagnosticMonitor.GetDefaultInitialConfiguration();
            // Set scheduled transfer interval for infrastructure logs to 1 minute  
            config.DiagnosticInfrastructureLogs.ScheduledTransferPeriod = System.TimeSpan.FromMinutes(1);    
            // Specify a logging level to filter records to transfer    
            config.DiagnosticInfrastructureLogs.ScheduledTransferLogLevelFilter = LogLevel.Error;    
            // Set scheduled transfer interval for user's Windows Azure Logs to 1 minute  
            config.Logs.ScheduledTransferPeriod = System.TimeSpan.FromSeconds(15);      
            DiagnosticMonitor.Start("DiagnosticsConnectionString", config); 
            RoleEnvironment.Changing += RoleEnvironmentChanging;
            System.Diagnostics.Trace.WriteLine("eDirectory WcfService Started");
            return base.OnStart();
        }

        private void RoleEnvironmentChanging(object sender, RoleEnvironmentChangingEventArgs e)
        {
            // If a configuration setting is changing
            if (e.Changes.Any(change => change is RoleEnvironmentConfigurationSettingChange))
            {
                // Set e.Cancel to true to restart this role instance
                e.Cancel = true;
            }
        }
    }
}
